package cn.darxs.noquestion.controller;

import cn.darxs.noquestion.common.Response;
import cn.darxs.noquestion.common.ResponseStateCode;
import cn.darxs.noquestion.entity.Renter;
import cn.darxs.noquestion.service.AdminService;
import cn.darxs.noquestion.service.RenterService;
import cn.darxs.noquestion.service.ReplierService;
import cn.darxs.noquestion.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/renterAccount")
public class RenterAccountController {
    @Autowired
    RenterService renterService;
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    ReplierService replierService;

    @GetMapping("/page/{index}/count/{count}")
    public Response getRenterByPage(HttpServletRequest request, @PathVariable long index, @PathVariable long count) {

        Object status = request.getSession().getAttribute("status");

        Response httpResponseEntity = new Response();
        if (status == null) {
            httpResponseEntity.setCode(ResponseStateCode.NO_LOGIN);
            httpResponseEntity.setMessage("客户未登录");
        } else if (!status.equals("admin")) {
            httpResponseEntity.setCode(ResponseStateCode.NOT_ADMIN);
            httpResponseEntity.setMessage("权限错误");
        } else {
            LambdaQueryWrapper<Renter> likeWrapper = new LambdaQueryWrapper<>();
            likeWrapper.orderByDesc(Renter::getCreateTime);
            IPage<Renter> page = new Page<>(index, count);
            IPage<Renter> allRenters = renterService.page(page, likeWrapper);
            if (allRenters == null) {
                httpResponseEntity.setCode(ResponseStateCode.RENTER_QUERY_FAILURE);
                httpResponseEntity.setMessage("分页查询失败");
            } else {
                httpResponseEntity.setCode(ResponseStateCode.OPERATION_SUCCEED);
                httpResponseEntity.setMessage("分页查询成功");
                httpResponseEntity.setData(allRenters);
            }
        }
        return httpResponseEntity;
    }

    @GetMapping("/page/{index}/count/{count}/with/{info}")
    public Response getRenterByPageWithInfo(HttpServletRequest request, @PathVariable long index, @PathVariable long count, @PathVariable String info) {
        Object status = request.getSession().getAttribute("status");
        Response httpResponseEntity = new Response();
        if (status == null) {
            httpResponseEntity.setCode(ResponseStateCode.NO_LOGIN);
            httpResponseEntity.setMessage("客户未登录");
        } else if (!status.equals("admin")) {
            httpResponseEntity.setCode(ResponseStateCode.NOT_ADMIN);
            httpResponseEntity.setMessage("权限错误");
        } else {
            LambdaQueryWrapper<Renter> likeWrapper = new LambdaQueryWrapper<>();
            likeWrapper.like(Renter::getUsername, info)
                    .or().like(Renter::getName, info)
                    .or().like(Renter::getInfo, info)
                    .orderByDesc(Renter::getCreateTime);
            IPage<Renter> page = new Page<>(index, count);
            IPage<Renter> allRenters = renterService.page(page, likeWrapper);
            if (allRenters == null) {
                httpResponseEntity.setCode(ResponseStateCode.RENTER_QUERY_FAILURE);
                httpResponseEntity.setMessage("分页查询失败");
            } else {
                httpResponseEntity.setCode(ResponseStateCode.OPERATION_SUCCEED);
                httpResponseEntity.setMessage("分页查询成功");
                httpResponseEntity.setData(allRenters);
            }
        }
        return httpResponseEntity;
    }

    @GetMapping("/all")
    public Response getAllRenter(HttpServletRequest request) {

        Object status = request.getSession().getAttribute("status");
        Response httpResponseEntity = new Response();
        if (status == null) {
            httpResponseEntity.setCode(ResponseStateCode.NO_LOGIN);
            httpResponseEntity.setMessage("客户未登录");
        } else if (!status.equals("admin")) {
            httpResponseEntity.setCode(ResponseStateCode.NOT_ADMIN);
            httpResponseEntity.setMessage("权限错误");
        } else {
            List<Renter> allRenters = renterService.list();
            if (allRenters == null) {
                httpResponseEntity.setCode(ResponseStateCode.RENTER_QUERY_FAILURE);
                httpResponseEntity.setMessage("查询失败");
            } else {
                httpResponseEntity.setCode(ResponseStateCode.OPERATION_SUCCEED);
                httpResponseEntity.setMessage("查询成功");
                httpResponseEntity.setData(allRenters);
            }
        }
        return httpResponseEntity;
    }

    @PutMapping("/create")
    public Response createAccount(HttpServletRequest request, @RequestBody Renter newRenter) {

        Object status = request.getSession().getAttribute("status");

        Response httpResponseEntity = new Response();
        if (status == null) {
            httpResponseEntity.setCode(ResponseStateCode.NO_LOGIN);
            httpResponseEntity.setMessage("客户未登录");
        } else if (!status.equals("admin")) {
            httpResponseEntity.setCode(ResponseStateCode.NOT_ADMIN);
            httpResponseEntity.setMessage("权限错误");
        } else {
            LambdaQueryWrapper<Renter> sameWrapper = new LambdaQueryWrapper<>();
            sameWrapper.eq(Renter::getUsername, newRenter.getUsername());
            Renter sameRenter = renterService.getOne(sameWrapper);

            LambdaQueryWrapper<Renter> adminWrapper = new LambdaQueryWrapper<>();
            adminWrapper.eq(Renter::getUsername, newRenter.getUsername());
            Renter adminRenter = renterService.getOne(adminWrapper);

            LambdaQueryWrapper<Renter> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(Renter::getUsername, newRenter.getUsername());
            Renter userRenter = renterService.getOne(userWrapper);

            LambdaQueryWrapper<Renter> replierWrapper = new LambdaQueryWrapper<>();
            replierWrapper.eq(Renter::getUsername, newRenter.getUsername());
            Renter replierRenter = renterService.getOne(replierWrapper);

            if (sameRenter == null && adminRenter == null && userRenter == null && replierRenter == null) {
                newRenter.clearSystemInfo();
                boolean isSuccess = renterService.save(newRenter);
                if (isSuccess) {
                    httpResponseEntity.setCode(ResponseStateCode.OPERATION_SUCCEED);
                    httpResponseEntity.setMessage("创建成功");
                } else {
                    httpResponseEntity.setCode(ResponseStateCode.RENTER_CREATE_FAILURE);
                    httpResponseEntity.setMessage("未知错误");
                }
            } else {
                httpResponseEntity.setCode(ResponseStateCode.RENTER_CREATE_FAILURE);
                httpResponseEntity.setMessage("账户名重复");
            }
        }

        return httpResponseEntity;
    }

    @PutMapping("/update")
    public Response updateAccount(HttpServletRequest request, @RequestBody Renter newRenter) {

        Object status = request.getSession().getAttribute("status");

        Response httpResponseEntity = new Response();
        if (status == null) {
            httpResponseEntity.setCode(ResponseStateCode.NO_LOGIN);
            httpResponseEntity.setMessage("客户未登录");
        } else if (!status.equals("admin")) {
            httpResponseEntity.setCode(ResponseStateCode.NOT_ADMIN);
            httpResponseEntity.setMessage("权限错误");
        } else {
            LambdaQueryWrapper<Renter> sameIDWrapper = new LambdaQueryWrapper<>();
            sameIDWrapper.eq(Renter::getId, newRenter.getId());
            Renter oldRenter = renterService.getOne(sameIDWrapper);

            if (oldRenter != null) {

                LambdaQueryWrapper<Renter> sameWrapper = new LambdaQueryWrapper<>();
                sameWrapper.eq(Renter::getUsername, newRenter.getUsername());
                Renter sameRenter = renterService.getOne(sameWrapper);

                LambdaQueryWrapper<Renter> adminWrapper = new LambdaQueryWrapper<>();
                adminWrapper.eq(Renter::getUsername, newRenter.getUsername());
                Renter adminRenter = renterService.getOne(adminWrapper);

                LambdaQueryWrapper<Renter> userWrapper = new LambdaQueryWrapper<>();
                userWrapper.eq(Renter::getUsername, newRenter.getUsername());
                Renter userRenter = renterService.getOne(userWrapper);

                LambdaQueryWrapper<Renter> replierWrapper = new LambdaQueryWrapper<>();
                replierWrapper.eq(Renter::getUsername, newRenter.getUsername());
                Renter replierRenter = renterService.getOne(replierWrapper);

                if ((sameRenter == null && adminRenter == null && userRenter == null && replierRenter == null)
                        || Objects.equals(Objects.requireNonNull(sameRenter).getId(), newRenter.getId())) {
                    newRenter.clearTimeInfo();
                    boolean success = renterService.updateById(newRenter);
                    if (success) {
                        httpResponseEntity.setCode(ResponseStateCode.OPERATION_SUCCEED);
                        httpResponseEntity.setMessage("更新成功");
                    } else {
                        httpResponseEntity.setCode(ResponseStateCode.RENTER_CREATE_FAILURE);
                        httpResponseEntity.setMessage("未知错误");
                    }
                } else {
                    httpResponseEntity.setCode(ResponseStateCode.RENTER_CREATE_FAILURE);
                    httpResponseEntity.setMessage("账户名重复");
                }
            } else {
                httpResponseEntity.setCode(ResponseStateCode.RENTER_ID_NOT_EXIST);
                httpResponseEntity.setMessage("账户不存在");
            }
        }

        return httpResponseEntity;
    }

    @DeleteMapping("/{id}")
    public Response deleteAccount(HttpServletRequest request, @PathVariable long id) {

        Object status = request.getSession().getAttribute("status");

        Response httpResponseEntity = new Response();
        if (status == null) {
            httpResponseEntity.setCode(ResponseStateCode.NO_LOGIN);
            httpResponseEntity.setMessage("客户未登录");
        } else if (!status.equals("admin")) {
            httpResponseEntity.setCode(ResponseStateCode.NOT_ADMIN);
            httpResponseEntity.setMessage("权限错误");
        } else {
            LambdaQueryWrapper<Renter> sameIDWrapper = new LambdaQueryWrapper<>();
            sameIDWrapper.eq(Renter::getId, id);
            Renter oldRenter = renterService.getOne(sameIDWrapper);

            if (oldRenter != null) {
                boolean success = renterService.removeById(id);
                if (success) {
                    httpResponseEntity.setCode(ResponseStateCode.OPERATION_SUCCEED);
                    httpResponseEntity.setMessage("删除成功");
                } else {
                    httpResponseEntity.setCode(ResponseStateCode.RENTER_CREATE_FAILURE);
                    httpResponseEntity.setMessage("未知错误");
                }
            } else {
                httpResponseEntity.setCode(ResponseStateCode.RENTER_ID_NOT_EXIST);
                httpResponseEntity.setMessage("账户不存在");
            }
        }

        return httpResponseEntity;
    }


}
